package com.songcha.library_base.mvvm.base

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.interfaces.IRefreshData
import com.songcha.library_base.interfaces.IResult
import com.songcha.library_base.mvvm.exception.CodeException
import com.songcha.library_base.mvvm.exception.LoginTokenInvalidException
import com.songcha.library_base.mvvm.exception.NoDataException
import com.songcha.library_base.mvvm.exception.NoNetworkException
import com.songcha.library_common.util.ToastUtil
import com.songcha.library_network.bean.BaseBean
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.net.ConnectException
import java.net.SocketTimeoutException



abstract class BaseRefreshViewModel<R:BaseRepository,RLST>(app: Application) : BaseViewModel<R>(app), IRefreshData {
    var isRefreshing=false
    var refresh_error:Throwable?=null
    val refresh_state : MutableLiveData<RefreshStateVM> = MutableLiveData<RefreshStateVM>()
    val rlst = MutableLiveData<MutableList<RLST>>()

    enum class RefreshStateVM{
        REFRESHING,
        REFRESH_SUCCESS,
        REFRESH_ERROR,
        REFRESH_TIMEOUT,
        REFRESH_NO_DATA,
        REFRESH_NO_NETWORK,
        REFRESH_CONNECT_ERROR,
    }

    override fun onRefreshing() {
        isRefreshing=true
        refresh_state.value=RefreshStateVM.REFRESHING
    }

    override fun onRefreshSuccess() {
        isRefreshing=false
        refresh_state.value=RefreshStateVM.REFRESH_SUCCESS
    }

    override fun onRefreshError(e: Throwable) {
        isRefreshing=false
        refresh_error=e
        refresh_state.value=RefreshStateVM.REFRESH_ERROR
    }

    override fun onRefreshNoData() {
        isRefreshing=false
        refresh_state.value=RefreshStateVM.REFRESH_NO_DATA
    }

    override fun onRefreshNoNetwork() {
        isRefreshing=false
        refresh_state.value=RefreshStateVM.REFRESH_NO_NETWORK
    }

    override fun onRefreshConnectError() {
        isRefreshing=false
        refresh_state.value=RefreshStateVM.REFRESH_CONNECT_ERROR
    }

    override fun onRefreshTimeOut() {
        isRefreshing=false
        refresh_state.value=RefreshStateVM.REFRESH_TIMEOUT
    }

    override fun onSetStateStateSubscribe() {
        if(isRefreshing){
            onRefreshing()
        }else{
            onLoading()
        }
    }

    override fun onSetStateNoNetwork() {
        if(isRefreshing){
            onRefreshNoNetwork()
        }else{
            onLoadNoNetwork()
        }
    }

    override fun onSetStateConnectError() {
        if(isRefreshing){
            onRefreshConnectError()
        }else{
            onLoadConnectError()
        }
    }

    override fun onSetStateNoData() {
        if(isRefreshing){
            onRefreshNoData()
        }else{
            onLoadNoData()
        }
    }

    override fun onSetStateTimeout() {
        if(isRefreshing){
            onRefreshTimeOut()
        }else{
            onLoadTimeOut()
        }
    }

    override fun onSetStateError(e: Throwable) {
        if(isRefreshing){
            onRefreshError(e)
        }else{
            onLoadError(e)
        }
    }

    override fun onSetStateSuccess() {
        if(isRefreshing){
            onRefreshSuccess()
        }else{
            onLoadSuccess()
        }
    }

    //empty implementation and implementation by handleDataListSuccess
    override fun onHandleSuccess(bean: Any, isSetState: Boolean) {

    }

    protected open fun handleDataListSuccess(newList:MutableList<RLST>){
        rlst.value = newList
        if(rlst.value!!.size==0){
            onSetStateNoData()
        }else{
            onSetStateSuccess()
        }
    }

}